class Book(val title: String, val author: String, val isbn: String) {  
  private var borrowed = false  
  
  // apply方法用于借书  
  def apply(): Unit = {  
    if (!borrowed) {  
      borrowed = true  
      println(s"书籍 '${title}' 已被借出。")  
    } else {  
      println(s"书籍 '${title}' 已经被借出了。")  
    }  
  }  
  
  // unapply方法用于还书  
  def unapply(): Unit = {  
    if (borrowed) {  
      borrowed = false  
      println(s"书籍 '${title}' 已归还。")  
    } else {  
      println(s"书籍 '${title}' 并未被借出，无需归还。")  
    }  
  }  
  
  // 检查书籍是否被借出  
  def isBorrowed: Boolean = borrowed  
}  
  
class Library {  
  import scala.collection.mutable.ListBuffer  
  
  private val availableBooks = new ListBuffer[Book]  
  private val borrowedBooks = new ListBuffer[Book]  
  
  // 添加新书到可用书籍列表  
  def addBook(book: Book): Unit = {  
    availableBooks += book  
    println(s"书籍 '${book.title}' 已添加到图书馆。")  
  }  
  
  // 借书方法，检查书籍是否可用，并将其移动到已借出列表  
  def borrowBook(title: String): Unit = {  
    val bookOption = availableBooks.find(_.title == title)  
    bookOption match {  
      case Some(book) =>  
        availableBooks -= book  
        borrowedBooks += book  
        book.apply() // 调用Book的apply方法借出书籍  
      case None =>  
        println(s"书籍 '${title}' 不在图书馆的可用书籍列表中。")  
    }  
  }  
  
  // 还书方法，检查书籍是否已借出，并将其移回可用书籍列表  
  def returnBook(title: String): Unit = {  
    val bookOption = borrowedBooks.find(_.title == title)  
    bookOption match {  
      case Some(book) =>  
        borrowedBooks -= book  
        availableBooks += book  
        book.unapply() // 调用Book的unapply方法归还书籍  
      case None =>  
        println(s"书籍 '${title}' 不在图书馆的已借出书籍列表中。")  
    }  
  }  
  
  // 打印当前书籍状态  
  def printStatus(): Unit = {  
    println("可用书籍列表:")  
    availableBooks.foreach(book => println(s"书名: ${book.title}, 作者: ${book.author}, ISBN: ${book.isbn}"))  
    println("\n已借出书籍列表:")  
    borrowedBooks.foreach(book => println(s"书名: ${book.title}, 作者: ${book.author}, ISBN: ${book.isbn}"))  
  }  
}  
  
// 使用示例  
object Main {  
  def main(args: Array[String]): Unit = {  
    val library = new Library()  
    val book1 = new Book("Scala编程", "张三", "9787121393254")  
    val book2 = new Book("Java核心技术", "李四", "9787115238268")  
  
    library.addBook(book1)  
    library.addBook(book2)  
    library.printStatus() // 打印初始书籍状态  
  
    library.borrowBook(book1.title) // 借出book1  
    library.printStatus() // 打印借出后的书籍状态  
  
    library.returnBook(book1.title) // 归还book1  
    library.printStatus() // 打印归还后的书籍状态  
  }  
}
